package com.framework.modules.job.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.entity.flow.FlowAccount;
import com.framework.entity.ter.TerMerchantReport;
import com.framework.service.AgentInfoService;
import com.framework.service.FlowAccountService;
import com.framework.service.MerchantService;
import com.framework.service.TransactionInfoService;
import com.framework.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component("xinChuangSpecialBenefitTask")
public class XinChuangSpecialBenefitTask {
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private AgentInfoService agentInfoService;
    @Autowired
    private TransactionInfoService transactionInfoService;

    @Autowired
    private FlowAccountService flowAccountService;
    /**
     * 信创88万分润
     */
    private void enoughDoubleEightBenefit(){

        List<TerMerchantReport> list = merchantService.selectList(
                new EntityWrapper<TerMerchantReport>()
                        .eq("dept_id", 889324L)
                        .eq("terminal_type",3));

        for (TerMerchantReport terMerchantReport :list) {
            //非星驿付跳过
            if(terMerchantReport.getTerminalType().intValue()!=3){
                continue;
            }
            Map<String, Object> result = transactionInfoService.querySumTranBySn(terMerchantReport.getSn(), terMerchantReport.getCreateDate());

            BigDecimal totalAmount=new BigDecimal(result.get("totalAmount").toString());

              FlowAccount account= flowAccountService.selectOne(
                    new EntityWrapper<FlowAccount>()
                            .eq("bel_id", terMerchantReport.getSn()));
              if(account!=null){
                  continue;
              }
            //如果交易额达到88万，则核实报备时间
            if(totalAmount.compareTo(new BigDecimal("880000.00"))!=-1){
                //比较最大时间和报备时间的月份
                Date reportDate=terMerchantReport.getCreateDate();
                Date lastDate= DateUtils.stringToDate(result.get("lastDate").toString(),DateUtils.DATE_PATTERN);
                Integer count = DateUtils.countMonth(reportDate, lastDate);
                //达标在10个月以内
                if(count<=10){
                    agentInfoService.addAmount(terMerchantReport.getAgentId(),new BigDecimal("200"));
                    FlowAccount flowAccount=new FlowAccount();
                    flowAccount.setBelId(terMerchantReport.getSn());
                    flowAccount.setAccountType(9);
                    flowAccount.setCreateTime(new Date());
                    flowAccount.setAmount(new BigDecimal("200"));
                    flowAccount.setSupportType(2);
                    flowAccount.setAgentId(terMerchantReport.getAgentId());
                    flowAccount.setProfit(totalAmount);
                    flowAccountService.insert(flowAccount);
                }
            }

        }

    }
}
